package com.chuangsi.project.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangsi.common.exception.ServiceException;
import com.chuangsi.common.utils.Assert;
import com.chuangsi.common.utils.ConfigUtil;
import com.chuangsi.common.utils.SecurityUtils;
import com.chuangsi.project.admin.domain.Seal;
import com.chuangsi.project.admin.dto.admin.seal.*;
import com.chuangsi.project.admin.dto.app.seal.ResAppSealListDto;
import com.chuangsi.project.admin.mapper.SealMapper;
import com.chuangsi.project.admin.service.SealService;
import com.chuangsi.project.wechat.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * E签宝印章 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-03-30
 */
@Service
public class SealServiceImpl extends ServiceImpl<SealMapper, Seal> implements SealService {

    @Autowired
    private ConfigUtil configUtil;
    @Autowired
    private PayUtil payUtil;



    @Override
    public List<ResSealSelectDto> select() {
        return list(Wrappers.lambdaQuery(Seal.class).select(Seal::getSealId,Seal::getSealName).eq(Seal::getStatus,"Y")).stream().map(ResSealSelectDto::new).collect(Collectors.toList());
    }

    @Override
    public List<ResAppSealListDto> getSealList() {
        return list(Wrappers.lambdaQuery(Seal.class).select(Seal::getSealId,Seal::getSealName,Seal::getOfflinePayPic,Seal::getOfflineStatus,Seal::getOnlineStatus,Seal::getEsignContract).eq(Seal::getStatus,"Y")).stream().map(x->{
            ResAppSealListDto resAppSealListDto = new ResAppSealListDto();
            resAppSealListDto.setSealId(x.getSealId());
            resAppSealListDto.setName(x.getSealName());
            resAppSealListDto.setOfflineStatus(x.getOfflineStatus());
            resAppSealListDto.setOnlineStatus(x.getOnlineStatus());
            resAppSealListDto.setEsignContract(x.getEsignContract());
            if(StrUtil.isNotBlank(x.getOfflinePayPic())){
                resAppSealListDto.setOffLinePayPic(configUtil.getUrl()+x.getOfflinePayPic());
            }
            return resAppSealListDto;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ResSealPageDto> pageQuery(ReqSealPageDto dto) {
        return baseMapper.pageQuery(dto);
    }

    @Override
    public ResSealInfoDto info(String sealId) {
        if(!SecurityUtils.isAdmin()){
            throw new ServiceException("admin账户才能查看");
        }
        Seal seal = getById(sealId);
        Assert.notNull(seal,"未找到印章");
        return BeanUtil.copyProperties(seal,ResSealInfoDto.class);
    }

    @Override
    public void addOrUpdate(ReqSealAddOrUpdateDto req) {
        if(!SecurityUtils.isAdmin()){
            throw new ServiceException("admin账户才能操作");
        }
        //如果是修改
        Seal seal = null;
        if(StrUtil.isNotBlank(req.getSealId())){
            seal = getById(req.getSealId());
            Assert.notNull(seal,"未找到印章");
            seal.setUpdateTime(LocalDateTime.now());
        }else {
            seal = new Seal();
            seal.setCreateTime(LocalDateTime.now());
        }
        BeanUtil.copyProperties(req,seal);
        seal.insertOrUpdate();
    }

    @Override
    public void changeStatus(ReqSealChangeStatusDto req) {
        if(!SecurityUtils.isAdmin()){
            throw new ServiceException("admin账户才能操作");
        }
        Seal seal = getById(req.getSealId());
        Assert.notNull(seal,"未找到印章");
        if(req.getType().equals(1)){
            seal.setStatus(req.getStatus());
        }
        if(req.getType().equals(2)){
            seal.setOnlineStatus(req.getStatus());
        }
        if(req.getType().equals(3)){
            seal.setOfflineStatus(req.getStatus());
        }
//        if(req.getType().equals(4)){
//            seal.setEsignContract(req.getStatus());
//        }
        seal.updateById();
    }


    @Override
    public void refreshConfiguration() {
        if(!SecurityUtils.isAdmin()){
            throw new ServiceException("admin账户才能操作");
        }
        payUtil.initConfig();
    }

}
